﻿using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace UIWindows.GlobalStatics
{
    [Attributes.Singleton]
    public class ReserveStatic
    {
        private PlateScanBarcodeMoeel _plateScanBarcodeStatic;

        public PlateScanBarcodeMoeel PlateScanBarcodeStatic
        {
            get { return _plateScanBarcodeStatic; }
            set
            {
                // 如果_plateScanBarcodeStatic不为null，移除旧对象的PropertyChanged事件处理器
                if (_plateScanBarcodeStatic != null)
                {
                    _plateScanBarcodeStatic.PropertyChanged -= PlateScanBarcodeStatic_PropertyChanged;
                }
                // 如果_plateScanBarcodeStatic不为null，移除旧对象的PropertyChanged事件处理器
                _plateScanBarcodeStatic = value;

                if (_plateScanBarcodeStatic != null)
                {
                    _plateScanBarcodeStatic.PropertyChanged += PlateScanBarcodeStatic_PropertyChanged;
                }
            }
        }

        public ReserveStatic()
        {
            try
            {
                PlateScanBarcodeStatic = !File.Exists(GlobalStatic.PlateScanBarcodePath)
                  ? new PlateScanBarcodeMoeel()
                  : JsonSerializer.Deserialize<PlateScanBarcodeMoeel>(File.ReadAllText(GlobalStatic.PlateScanBarcodePath, Encoding.UTF8));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        // 当PropertyChanged事件被触发时，调用Save()方法
        private void PlateScanBarcodeStatic_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Save();
        }

        public void Save() => File.WriteAllText(GlobalStatic.PlateScanBarcodePath, JsonSerializer.Serialize(PlateScanBarcodeStatic, GlobalStatic.Options), Encoding.UTF8);
    }

    public class PlateScanBarcodeMoeel : INotifyPropertyChanged
    {
        private string _trayBarcode;
        private long _trayBarcodeID;
        private DateTime _trayBarcodeTime;

        public event PropertyChangedEventHandler PropertyChanged;

        public string TrayBarcode
        {
            get { return _trayBarcode; }
            set
            {
                _trayBarcode = value;
                OnPropertyChanged(nameof(TrayBarcode));
            }
        }

        public long TrayBarcodeID
        {
            get { return _trayBarcodeID; }
            set
            {
                _trayBarcodeID = value;
                OnPropertyChanged(nameof(TrayBarcodeID));
            }
        }

        public DateTime TrayBarcodeTime
        {
            get { return _trayBarcodeTime; }
            set
            {
                _trayBarcodeTime = value;
                OnPropertyChanged(nameof(TrayBarcodeTime));
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
